# TILESET ORGANIZING

### example: BG Layer 1 of grasslands areas (Mario's Pad, Mushroom Way)

## A. organize 16x16 tiles from decompressed data

1. DECOMPRESS TILESET
   example: Grasslands BG Layer 1
   - the compressed tileset data starts at $3BF474
   - the decompressed tileset is 0x1000 (4096) bytes
   decompressed tileset:
   - the selection represents the data for the first 16 tiles in the tileset
   - each 16x16 tile is 8 bytes
   - 16x16 tiles are not organized consecutively; that is, the top two 8x8 tiles of the 16x16 tile are on the first row and the bottom two are 40 bytes ahead (ie.on the next row)
   - in the decompressed data, the 8x8 tiles are organized similar to the graphics seen in a tile editor
   - every 2 bytes defines an 8x8 tile plus its palette and/or its mirroring
   - Here are the first 16 tiles in the tileset:
   - this corresponds directly with the hex data a few lines up
   - for example...
     this data:
     represents tile 0x0002:
   - in total for this tileset, there are 1024 tiles in all
   - 00 00 represents a blank transparent 8x8 tile
2. ORGANIZE 16x16 tile
   example: tile 0x03
   - every 2 bytes defines an 8x8 tile, as mentioned before
   - 51 2D and 52 29 are these:
   - 61 29 and 62 29 are these:
   - the top row (51 2D and 52 29) and the bottom row (61 29 and 62 29) are 40 bytes apart
   - every 0x80 bytes represents 16 tiles

   - byte 1 is the number of the 8x8 tile in the graphic set
   - byte 2 defines the graphic set, palette and mirroring

## B. define graphic and palette of tiles

1. LOAD GRAPHIC SETS
   - these 5 decompressed graphics sets are used by Mario's Pad
   - the addresses shown above the images are the locations of the compressed graphic sets
   - the 8x8 graphics tiles in the graphics sets are arranged into 16x16 tiles by the tileset data

   $0E60EA graphics set 0x40 | $0E7E77 graphics set 0x41 | $0E8C8E graphics set 0x42 | $0E994B graphics set 0x43 | $0EA5CB graphics set 0x44

2. DEFINE GRAPHIC SET OF 8x8 TILE
   here is the decompressed data for the Mario's Pad tileset:
   example: 51 2D
   - the particular graphic sets used in this case are 0x40, 0x41, 0x42, 0x43, and 0x44
   - these graphic sets will be referred to as sections 0, 1, 2, 3, and 4
   - eg. section 0 is the first graphic set (0x40)
     section 1 is the second graphic set (0x41)
     etc...
   The section (graphic set) is determined through the remainder after AND'ing the byte with 0x1F then dividing it by 4.
   example: the remainder of 0x2D AND'ed with 0x1F then divided by 4 is 1, therefore the tile is from section 1 (graphic set 0x41)
   Thus, the tile is number 0x51 of graphic set 0x41:
3. DEFINE PALETTE OF 8x8 TILE
   The palette of an 8x8 tile is assigned from the area's palette set.
   This is the Grasslands palette set, which is assigned to Mario's Pad:
   These are referred to as palettes 0 - 7.
   The palette is determined through the quotient after AND'ing the byte with 0x1F then dividing it by 4.
   example: the quotient of 0x2D AND'ed with 0x1F then divided by 4 is 3, therefore it uses palette 3
   is after applying the palette
4. MIRRORING AND INVERTING
   - to mirror the 8x8 tile, simply set bit 6 of byte 2
   - to invert (horizontal flip) the 8x8 tile, simply set bit 7 of byte 2
   - the use of bit 5 in byte 2 is yet to be determined (it doesn't seem to do anything), but it is set in the tile 51 2D
   The 8x8 tile 51 2D is neither mirrored or inverted. To mirror it, change 0x2D to 0x6D. To invert it, change 0x2D to 0xA2. To mirror and invert it, change 0x2D to 0xED.